package com.hc.sort.server.view;

import java.io.*;
import java.net.*;
import java.util.*;

import com.hc.sort.server.pojo.BusinessWindow;
import com.hc.sort.server.pojo.Caller;
import com.hc.sort.server.pojo.Ticket;
import com.hc.sort.server.service.BusinessTypeService;
import com.hc.sort.server.service.BusinessTypeServiceImpl;
import com.hc.sort.server.service.TicketService;
import com.hc.sort.server.service.TicketServiceImpl;

import sun.print.resources.serviceui;

public class Server implements Runnable {
	ServerSocket serverSocket;
	ServerUI serverUI;
	
	public Server(ServerUI serverUI) {
		this.serverUI=serverUI;
	}
	
	@Override
	public void run() {
		System.out.println("===银行排队系统----服务端启动===");
		
		try {
			serverSocket = new ServerSocket(6666);
			
			while(true) {
				Socket socket = serverSocket.accept();
				//处理请求
				new Thread(new RequestHandler(socket,serverUI)).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭已启动的服务器
	 */
	public void stopServer() {
		try {
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

/**
 * 
 * @author 11467
 *请求处理器，处理叫号端，取号端的请求
 */
class RequestHandler implements Runnable{
	ServerUI serverUI;
	TicketService ticketService =new TicketServiceImpl();
	BusinessTypeService businessTypeService=new BusinessTypeServiceImpl();
	Socket socket;
	
	public RequestHandler(Socket socket,ServerUI serverUI) {
		this.socket=socket;
		this.serverUI=serverUI;
	}
	
	@Override
	public void run() {
		//获取客户端的数据
		try {
			//因为为了处理的方便，客户端会直接传输对象过来
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			
			//客户端提交的信息分封装在map中
			HashMap map =(HashMap) ois.readObject();
			//返回客户端的数据也分是封装在map中
			HashMap retMap = new HashMap();
			
			//command就是需要调用的服务
			String command =(String) map.get("command");
			String ip=socket.getRemoteSocketAddress().toString();
			Ticket ticket;
			
			//写日志
			serverUI.writeLog("command:"+command+",ip:"+ip);
			if(command.equals("quhao")) {
				ticket = ticketService.takeTicket((String) map.get("arg0"),ip);
				
				retMap.put("data1", ticket);
				retMap.put("result",0);
				retMap.put("message","处理成功");
			}else if ("businessList".equals(command)) {
				List businessList=businessTypeService.findAll();
				retMap.put("data1", businessList);
				retMap.put("result",0);
				retMap.put("message","处理成功");
			}else if ("jiaohao".equals(command)) {
				//获取参数
				BusinessWindow busWin = (BusinessWindow) map.get("arg0");
				Caller caller = (Caller) map.get("arg1");
				//调用业务层方法，实现叫号逻辑
				ticket = ticketService.callTicket(busWin,caller,ip);
				
				if (ticket!=null) {
					retMap.put("arg0", ticket);
					retMap.put("result",0);
					retMap.put("message","处理成功");
				}else {
					retMap.put("result",-1);
					retMap.put("message","没有号需要处理");
				}
			}else if ("chongjiao".equals(command)) {
				int ticketId=(int) map.get("arg0");
				ticketService.recall(ticketId);
			}else if ("handlingSuccess".equals(command)) {
				int ticketId=(int) map.get("arg0");
				int result=(int) map.get("arg1");
				ticketService.handleFinished(ticketId, result, command);
			}else if ("handlingFailure".equals(command)) {
				int ticketId=(int) map.get("arg0");
				int result=(int) map.get("arg1");
				ticketService.handleFinished(ticketId, result, command);
			}
			
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(retMap);
			
			ois.close();
			oos.close();
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
